package com.macken.wow.util;


import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Session;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;


public class EmailUtil {

	public static void sendEmail(String from, String to, String content) {
		try {
			Properties pro = new Properties();
			pro.put("mail.smtp.host", "smtp.gmail.com");
			pro.put("mail.smtp.auth", "true");
			pro.put("mail.smtp.starttls.enable", "true");
			pro.put("mail.smtp.port", "587");
			Session session = Session.getInstance(pro, new Authenticator() {
				@Override
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("wowheavenservice@gmail.com", "Wowheaven");
				}
			});
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
			message.setSubject("test");
			message.setText(content);
			message.saveChanges();
			Transport tran = session.getTransport("smtp");
			tran.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
//		String from = "wowheavenservice@gmail.com";
//		String to = "1505014246@qq.com";
//		String content = "<a href=\"www.baidu.com\" target=\"_blank\">test</a>";
//		//		sendEmail(from, to, content);
//		HtmlEmail email = new HtmlEmail();
//		email.setHostName("smtp.gmail.com");//邮件服务器
//		email.setSmtpPort(587);
//		email.setTLS(true);
//		email.setAuthentication("wowheavenservice@gmail.com", "Wowheaven");//smtp认证的用户名和密码
//		email.addTo("403332023@qq.com", "兔子");//收信者
//		email.setFrom("wowheavenservice@gmail.com", "猫咪");//发信者
//		email.setSubject("猫咪的测试邮件");//标题
//		email.setCharset("UTF-8");//编码格式
//		email.setHtmlMsg(content);
//		email.send();//发送
	}
	public static void send(String to, String subject, String content) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setHostName("smtp.gmail.com");//邮件服务器
			email.setSmtpPort(587);
			email.setTLS(true);
			email.setAuthentication("wowheavenservice@gmail.com", "Wowheaven");//smtp认证的用户名和密码
			email.addTo(to, "");//收信者
			email.setFrom("wowheavenservice@gmail.com", "wow-heaven");//发信者
			email.setSubject(subject);//标题
			email.setCharset("UTF-8");//编码格式
			email.setHtmlMsg(content);
			email.send();//发送
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
